library(sandwich)
library(foreign)
library(lmtest)
library(stargazer)
setwd("~/Replication Code/")

##---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

ccap08 <- read.dta("./data/ccap08_subset.dta")

ccap12 <- read.dta("./data/ccap12_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")

# Subsetting Data
anes9294.sub <- na.omit(anes9297[c("white_int92", "white_int94",
                                    "pid92_7scaled", "pid94_7scaled", "rr92_scaled", "rr94_scaled", "V940005")])
anes9294.sub <- subset(anes9294.sub, white_int92 == white_int94, 
                       select = c("pid92_7scaled", "pid94_7scaled", "rr92_scaled", "rr94_scaled", "V940005"))
anes9294.sub$df <- "anes9294"

ccap08.sub <- na.omit(ccap08[c("pid7r_m_sc", "rr_m_scaled", "rr_o_scaled", "pid7r_o_sc", "weight")]) 
ccap08.sub$df <- "ccap08"

ccap12.p2 <- subset(ccap12, p2 == 1, 
                    select=c("rr_p1_scaled", "rr_p2_scaled", "pp_pid7r_sc", "pid7_sc", "weight"))
ccap12.p2 <- na.omit(ccap12.p2)
ccap12.p2$df <- "ccap12.p2"

ccap12.p3 <- subset(ccap12, p3 == 1, 
                    select=c("rr_p1_scaled", "rr_p3_scaled", "pp_pid7r_sc", "pid7_sc", "weight"))
ccap12.p3 <- na.omit(ccap12.p3)
ccap12.p3$df <- "ccap12.p3"

ccap16.sub <- na.omit(ccap16[c("b_rr_sc", "p_rr_sc", "b_pid7_sc", "p_pid7_sc", "weight_post")])
ccap16.sub$df <- "ccap16"

vsgdat.sub <- na.omit(vsg.dat[c("rr_sc_12", "rr_sc_16", "pid7_sc_12", "pid7_sc_16", "weight")])
vsgdat.sub$df <- "vsg"

# Common Names
names(anes9294.sub) <- c("b_pid", "p_pid", "b_rr", "p_rr", "weight", "df")
names(ccap08.sub) <- c("b_pid", "b_rr", "p_rr", "p_pid", "weight", "df")
names(ccap12.p2) <- c("b_rr", "p_rr", "b_pid", "p_pid", "weight", "df")
names(ccap12.p3) <- c("b_rr", "p_rr", "b_pid", "p_pid", "weight", "df")
names(ccap16.sub) <- c("b_rr", "p_rr", "b_pid", "p_pid", "weight", "df")
names(vsgdat.sub) <- c("b_rr", "p_rr", "b_pid", "p_pid", "weight", "df")

# Stacking Data
cdf <- rbind(anes9294.sub, ccap08.sub, ccap12.p2, 
             ccap12.p3, ccap16.sub, vsgdat.sub)
cdf$df <- factor(cdf$df, levels = c("anes9294", "ccap08", "ccap12.p2", "ccap12.p3", "vsg", "ccap16"))


# PID on RR
m1 <- lm(p_rr ~  b_pid*df  + b_rr*df, 
         data = cdf, weights = weight)
summary(m1)
vhat.m1 <- vcovHC(m1, type = "HC1")
coeftest(m1, vcov = vhat.m1)
rse.m1 <- sqrt(diag(vhat.m1))

# RR on PID
m10 <- lm(p_pid ~  b_pid*df  + b_rr*df, 
          data = cdf, weights = weight)
summary(m10)
vhat.m10 <- vcovHC(m10, type = "HC1")
coeftest(m10, vcov = vhat.m10)
rse.m10 <- sqrt(diag(vhat.m10))


#----------------------------------------------------------------------------------------------------#
stargazer(m1, m10, title = "Relationship between Partisanship and Racial Attitudes",
          covariate.labels = c("Partisanship$_{t-1}$", "CCAP 2008", "CCAP 2012: March",
                               "CCAP 2012: August", "VOTER Survey 2012-2016", "CCAP 2016", 
                               "Racial Resentment$_{t-1}$",
                               "Partisanship$_{t-1}$*CCAP 2008",
                               "Partisanship$_{t-1}$*CCAP 2012: March",
                               "Partisanship$_{t-1}$*CCAP 2012: August",
                               "Partisanship$_{t-1}$*VOTER Survey 2012-2016",
                               "Partisanship$_{t-1}$*CCAP 2016",
                               "Racial Resentment$_{t-1}$*CCAP 2008",
                               "Racial Resentment$_{t-1}$*CCAP 2012: March",
                               "Racial Resentment$_{t-1}$*CCAP 2012: August",
                               "Racial Resentment$_{t-1}$*VOTER Survey 2012-2016",
                               "Racial Resentment$_{t-1}$*CCAP 2016"),
          dep.var.labels = c("Racial Resentment", "Partisanship"), 
          se = list(rse.m1, rse.m10),
          no.space = T, notes = c("OLS regression results. Robust errors in parentheses."), 
          notes.align = "l", intercept.bottom = T,
          df= F,
          digits = 3, omit.stat = c("f", "adj.rsq"), align = T, star.char = c("*"), star.cutoffs = c(0.05)
)
